﻿using HaoCoding.DCdb.General.IO;
using HaoCoding.DCdb.Storage;
using HaoCoding.DCdb.WaterfallTree;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HaoCoding.DCdb.Database
{
    /// <summary>
    /// 数据库实例化类
    /// </summary>
    public static class STSdb
    {
        /// <summary>
        /// 通过数据堆建立存储引擎
        /// </summary>
        /// <param name="heap">数据堆接口</param>
        /// <returns>存储引擎</returns>
        public static IStorageEngine FromHeap(IHeap heap)
        {
            return new StorageEngine(heap);
        }

        /// <summary>
        /// 通过文件流建立存储引擎
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <returns>存储引擎</returns>
        public static IStorageEngine FromStream(Stream stream)
        {
            IHeap heap = new Heap(stream, false, AllocationStrategy.FromTheCurrentBlock);

            return FromHeap(heap);
        }

        /// <summary>
        /// 从内存中建立存储引擎
        /// </summary>
        /// <returns></returns>
        public static IStorageEngine FromMemory()
        {
            var stream = new MemoryStream();

            return FromStream(stream);
        }

        /// <summary>
        /// 通过指定文件建立存储引擎
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>存储引擎</returns>
        public static IStorageEngine FromFile(string fileName)
        {
            var stream = new OptimizedFileStream(fileName, FileMode.OpenOrCreate);  //OpenOrCreate:打开文件如果存在则直接打开，否则创建新文件。

            return FromStream(stream);
        }

    }
}
